﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CellarDoor.Gui.Elements;
using CellarDoor.Gui.Animation;
using Microsoft.Xna.Framework;

namespace CellarDoor.Gui.Demo.Xbox
{
    public class DashboardMenu : UiElement
    {
        private List<UiLabel> labels = new List<UiLabel>();
        private List<DashboardPreview> preview = new List<DashboardPreview>();
        int index;

        public int SelectedIndex
        {
            get
            {
                return index;
            }
        }

        public DashboardPreview SelectedItem
        {
            get
            {
                if (index >= 0 && index < preview.Count)
                {
                    return preview[index];
                }
                return null;
            }
        }

        public DashboardMenu(UiElement parent) 
            : base(parent)
        {
            Initialize();
        }

        private void Initialize()
        {
        }

        public DashboardPreview AddEntry(string text)
        {
            var label = new UiLabel(this);
            label.Text = text;
            label.Width = 200;
            label.Height = 30;
            label.TextColor = Vector3.Zero;
            labels.Add(label);

            var prev = new DashboardPreview(this);
            prev.Top = 300;
            prev.Left = 0;
            prev.Width = 800;
            prev.Height = 300;
            preview.Add(prev);

            StartShift();

            return prev;
        }

        public void ShiftNext()
        {
            index++;
            if (index >= labels.Count)
            {
                index = 0;
            }
            StartShift();
        }

        public void ShiftPrev()
        {
            index--;
            if (index < 0)
            {
                index = labels.Count - 1;
            }
            StartShift();
        }

        private void StartShift()
        {
            for (int i = 0; i < labels.Count; i++)
            {
                if (i == index)
                {
                    preview[i].FadeIn(250);
                }
                else
                {
                    preview[i].FadeOut(250);
                }

                int id = i - 1 + index;
                if (id < 0)
                {
                    id += labels.Count;
                }
                if (id >= labels.Count)
                {
                    id -= labels.Count;
                }

                var label = labels[id];

                if (i == 0)
                {
                    label.FadeTo(0, 250);
                }
                else
                {
                    float opacity = (float)MathHelper.Clamp(5 - i, 0, 5) / 5f;
                    label.FadeTo(opacity, 250);
                }
                
                
                label.SlideTo(new Vector2(0, 300 - (i + 1) * label.Height), 250);
            }
        }
    }
}
